package com.taptech.doufu.services;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.taptech.doufu.util.TTLog;
import com.taptech.doufu.ui.activity.NewCpDesActivity;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.net.httputils.HttpRequestObject;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.net.httputils.HttpUtil;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Administrator on 2015/7/10.
 */
public class CpService implements HttpResponseListener
{
    public static final String BASE_URL = "http://" + Constant.HOST_NAME + "/index.php/post/";


    private static CpService instance = new CpService();


    public static final int HANDLE_LOAD_SINGLE_CP_CONTENT = 3003;
    public static final int HANDLE_TYPE_ADD_CP = 3004;
    public static final int HANDLE_TYPE_CANCEL_CP = 3005;
    public static final int HANDLE_LOAD_PERSON_CPS_LIST = 3006;
    public static final int HANDLE_LOAD_PERSONAL_CPS_CONTENT = 3007;
    public static final int HANDLE_LOAD_HOT_CPS = 3008;
    public static final int HANDLE_LOAD_SEARCH_CPS = 3009;
    public static final int HANDLE_TYPE_LOAD_SINGLE_CP = 3011;
    public static final int HANDLE_TYPE_LOAD_SINGLE_CP_JOURNALS = 3012;


    public static CpService getInstance()
    {
        return instance;
    }




    @Override
    public void handleResponse(int handleType, HttpResponseObject response)
    {
        // TODO Auto-generated method stub

    }


    public void loadPersonalCPs(String user_id, HttpResponseListener listener)
    {
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_LOAD_PERSON_CPS_LIST);
        request.setUrl(Constant.BASE_URL + "/cps/mine?user_id=" + user_id);
        request.setListener(listener);
        HttpUtil.sendGetRequest(request);

    }

      /**
       * 用户关注的cp数据
       */
    public void loadPersonalCPContent(String type, int page, HttpResponseListener listener)
    {
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_LOAD_PERSONAL_CPS_CONTENT);
        try
        {
            request.setUrl(Constant.BASE_URL + "search/cps?types=" + type + "&size=" + Constant.SEPARATE_RETURN_DATA_NUMBER + "&begin=" + (page * Constant.SEPARATE_RETURN_DATA_NUMBER));

            if (listener != null)
            {
                request.setListener(listener);
            } else
            {
                request.setListener(this);
            }
            HttpUtil.sendGetRequest(request);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public void addCp(String cp_id, HttpResponseListener listener)
    {
        try
        {
            HttpRequestObject request = new HttpRequestObject();
            request.setHandleType(HANDLE_TYPE_ADD_CP);
            List<NameValuePair> lsit = new LinkedList<NameValuePair>();
            NameValuePair value;
            value = new BasicNameValuePair("id", cp_id);
            lsit.add(value);
            request.setUrl(Constant.BASE_URL + "cps/follow");
            request.setListener(listener);
            request.setData(lsit);
            HttpUtil.sendPostRequest(request);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * 取消关注cp
     */
    public void cancelCp(String cp_id, HttpResponseListener listener)
    {
        try
        {
            HttpRequestObject request = new HttpRequestObject();
            request.setHandleType(HANDLE_TYPE_CANCEL_CP);
            List<NameValuePair> lsit = new LinkedList<NameValuePair>();
            NameValuePair value;
            value = new BasicNameValuePair("id", cp_id);
            lsit.add(value);
            request.setUrl(Constant.BASE_URL + "cps/unfollow");
            request.setListener(listener);
            request.setData(lsit);
            HttpUtil.sendPostRequest(request);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }


    public void loadHotCps(HttpResponseListener listener)
    {
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_LOAD_HOT_CPS);
        request.setUrl(Constant.BASE_URL + "cps/hot");
        request.setListener(listener);
        HttpUtil.sendGetRequest(request);

    }


    /*路径：/cps/search
    方法：GET
    参数：
    name: CP名称
    last: size:*/
    public void searchCps(String name, String last, HttpResponseListener listener)
    {
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_LOAD_SEARCH_CPS);
        request.setUrl(Constant.BASE_URL + "cps/search?name=" + name + "&last=" + last);
        request.setListener(listener);
        HttpUtil.sendGetRequest(request);

    }

    /**
     * 记录搜索关键词与+萌CP关联
     */
    public void recordSearchCp(HttpResponseListener listener, String from, String to)
    {
        try
        {
            if (from.equals(""))
            {
                from = to;
            }
            TTLog.i("TAG", "from:" + from);
            HttpRequestObject request = new HttpRequestObject();
            List<NameValuePair> lsit = new LinkedList<NameValuePair>();
            NameValuePair value;
            value = new BasicNameValuePair("from", from);
            lsit.add(value);
            value = new BasicNameValuePair("to", to);
            lsit.add(value);

            request.setUrl(Constant.BASE_URL + "cps/saveSearch");
            request.setListener(listener);
            request.setData(lsit);
            HttpUtil.sendPostRequest(request);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * 获取单个标签信息
     */
    public void loadSingleCpInfo(HttpResponseListener listener, String id, String name)
    {
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_TYPE_LOAD_SINGLE_CP);
        try
        {
            if (id == null)
            {
                request.setUrl(Constant.BASE_URL + "/tags/one?name=" + URLEncoder.encode(name, "utf-8"));
            } else
            {
                request.setUrl(Constant.BASE_URL + "/tags/one?id=" + id);
            }
        } catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        request.setListener(listener);
        HttpUtil.sendGetRequest(request);
    }

    /**
     * 获取单个标签信息
     */
    public void loadSingleCpJournals(String id, String last, HttpResponseListener listener)
    {
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_TYPE_LOAD_SINGLE_CP_JOURNALS);
        request.setUrl(Constant.BASE_URL + "journals/topic?tag_id=" + id + "&size=" + Constant.PAGE_SIZE_20 + "&last=" + last);
        request.setListener(listener);
        HttpUtil.sendGetRequest(request);
    }

    public static void enterCpDesActivity(Context mContext, String id)
    {
        Intent intent = new Intent();
        intent.setClass(mContext, NewCpDesActivity.class);
        intent.putExtra(Constant.TOPIC_ID, id);
        intent.putExtra(Constant.TYPE, NewCpDesActivity.TYPE_CP);
        mContext.startActivity(intent);
    }

    public void addUserTakeInTag(String tag_id, HttpResponseListener listener)
    {
        try
        {
            HttpRequestObject request = new HttpRequestObject();
            List<NameValuePair> lsit = new LinkedList<NameValuePair>();
            NameValuePair value;
            value = new BasicNameValuePair("tag_id", tag_id);
            lsit.add(value);
            request.setUrl(Constant.BASE_URL + "tags/participate");
            request.setListener(listener);
            request.setData(lsit);
            HttpUtil.sendLockedPostRequest(request, (Activity) listener);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}
